package com.his.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.his.struts.dao.function.FunctionDao;
import com.his.struts.dao.function.FunctionImp;
import com.his.struts.form.FunctionForm;



public class FunctionAction extends DispatchAction {

	FunctionDao dao = new FunctionImp();
	
	public ActionForward addUserFunction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		FunctionForm functionForm = (FunctionForm) form;
		// 添加用户功能
		dao.addUserFunction(functionForm);
		
		// 得到用户所拥有的功能
		List<FunctionForm> userFunction = dao.getUserFunction();
		// 把功能设置到全局
		this.getServlet().getServletContext().setAttribute("userFunction", userFunction);
		request.setAttribute("message", "添加成功");
		return mapping.getInputForward();
	}
	
	public ActionForward addDoctorFunction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		FunctionForm functionForm = (FunctionForm) form;
		// 添加医生功能
		dao.addDoctorFunction(functionForm);
		
		// 得到用户所拥有的功能
		List<FunctionForm> doctorFunction = dao.getDoctorFunction();
		// 把功能设置到全局
		this.getServlet().getServletContext().setAttribute("doctorFunction", doctorFunction);
		request.setAttribute("message", "添加成功");
		return mapping.getInputForward();
	}
	
	public ActionForward delUserFunction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		FunctionForm functionForm = (FunctionForm) form;
		
		int fid = functionForm.getFid();
		// 如果功能id不为0
		if(fid!=0){
			// 删除该功能
			dao.delUserFunction(fid);
			request.setAttribute("message", "删除成功");
		}
		
		// 得到用户所拥有的功能
		List<FunctionForm> userFunction = dao.getUserFunction();
		// 把功能设置到全局
		this.getServlet().getServletContext().setAttribute("userFunction", userFunction);
		return mapping.getInputForward();
	}
	
	public ActionForward delDoctorFunction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		FunctionForm functionForm = (FunctionForm) form;
		int fid = functionForm.getFid();
		// 如果功能id不为0
		if(fid!=0){
			// 删除该功能
			dao.delDoctorFunction(fid);
			request.setAttribute("message", "删除成功");
		}
		
		// 得到用户所拥有的功能
		List<FunctionForm> doctorFunction = dao.getDoctorFunction();
		// 把功能设置到全局
		this.getServlet().getServletContext().setAttribute("doctorFunction", doctorFunction);
		
		return mapping.getInputForward();
	}
	
	public ActionForward updateUserFunction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		FunctionForm functionForm = (FunctionForm) form;
		// 更新用户功能
		dao.updateUserFunction(functionForm);
		
		// 得到用户所拥有的功能
		List<FunctionForm> userFunction = dao.getUserFunction();
		// 把功能设置到全局
		this.getServlet().getServletContext().setAttribute("userFunction", userFunction);
		request.setAttribute("message", "修改成功");
		return mapping.getInputForward();
	}
	
	public ActionForward updateDoctorFunction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		FunctionForm functionForm = (FunctionForm) form;
		// 更新医生功能
		dao.updateDoctorFunction(functionForm);
		
		// 得到用户所拥有的功能
		List<FunctionForm> doctorFunction = dao.getDoctorFunction();
		// 把功能设置到全局
		this.getServlet().getServletContext().setAttribute("doctorFunction", doctorFunction);
		request.setAttribute("message", "修改成功");
		return mapping.getInputForward();
	}
	
}